﻿using MediaBot.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace RakutenProxy
{
    /// <summary>
    /// Convert string to currency
    /// </summary>
    public class Utils
    {
        private static string RemoveVietnameseCharacter(string input)
        {
            var clone = new StringBuilder(input);

            string[] a = { "a", "á", "ả", "ã", "ạ", "à", "ă", "ắ", "ẳ", "ẵ", "ặ", "ằ", "â", "ấ", "ẩ", "ẫ", "ậ", "ầ" };
            string[] u = { "u", "ú", "ủ", "ũ", "ụ", "ù", "ư", "ứ", "ử", "ữ", "ự", "ừ" };
            string[] o = { "o", "ó", "ỏ", "õ", "ọ", "ò", "ô", "ố", "ổ", "ỗ", "ộ", "ồ", "ơ", "ớ", "ở", "ỡ", "ợ", "ờ" };
            string[] i = { "i", "í", "ỉ", "ĩ", "ị", "ì" };
            string[] y = { "y", "ý", "ỷ", "ỹ", "ỵ", "ỳ" };
            string[] e = { "e", "é", "ẻ", "ẽ", "ẹ", "è", "ê", "ế", "ể", "ễ", "ệ", "ề" };
            string[] d = { "d", "đ" };

            string[][] unicodes = { a, u, o, i, y, e, d };

            foreach (var array in unicodes)
            {
                for (int j = 1; j < array.Length; j++)
                {
                    var c = array[j];
                    if (input.Contains(c))
                        clone = clone.Replace(c, array[0]);
                }
            }

            return clone.ToString();
        }

        public static string UniformUrl(string input)
        {
            var asciiText = RemoveVietnameseCharacter(input);
            asciiText = Regex.Replace(asciiText, @"([^\u0000-\u007F]|[^\w\d\s])+", "");
            asciiText = Regex.Replace(asciiText, @"\s+", "_");

            return asciiText;
        }

        public static decimal GetDecimalFromString(string input)
        {
            if (string.IsNullOrEmpty(input))
                return 0;
            var regex = new Regex(@"[^\d]*");
            var replace = regex.Replace(input, "");

            decimal result;

            if (!decimal.TryParse(replace, out result))
                result = 0;

            return result;
        }

        public static string BuildErrorMessage(MethodBase methodInfo, Guid linkConfigGuid, int linkId)
        {
            var builder = new StringBuilder();
            try
            {
                builder.AppendFormat("Invoked method: {0}.{1}", methodInfo.ReflectedType, methodInfo.Name);
                builder.AppendLine("LinkConfigGuid: " + linkConfigGuid.ToString());
                builder.AppendLine("LinkId: " + linkId);
            }
            catch
            {
            }

            return builder.ToString();
        }

        public static ArrayList GetDefaultNamespaces()
        {
            var ns = new NamespaceUriItem { Name = "ns", Value = "http://www.w3.org/1999/xhtml" };

            return GetDefaultNamespaces(ns);
        }

        public static ArrayList GetDefaultNamespaces(params NamespaceUriItem[] nsItems)
        {
            return new ArrayList(nsItems);
        }
    }
}
